package com.ruoyi.appointment.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.appointment.domain.vo.BusServiceItemVo;

import static com.ruoyi.appointment.enums.ServiceItemAuditStatusEnum.*;

import com.ruoyi.appointment.domain.vo.ServiceItemAuditInfo;
import com.ruoyi.appointment.enums.ServiceItemAuditStatusEnum;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.flow.service.IBusBpmnInfoService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.appointment.mapper.BusServiceItemMapper;
import com.ruoyi.appointment.domain.BusServiceItem;
import com.ruoyi.appointment.service.IBusServiceItemService;
import org.springframework.util.Assert;

/**
 * 服务项Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-05
 */
@Service
public class BusServiceItemServiceImpl implements IBusServiceItemService {
    @Autowired
    private BusServiceItemMapper busServiceItemMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IBusBpmnInfoService busBpmnInfoService;

    /**
     * 查询服务项
     *
     * @param id 服务项主键
     * @return 服务项
     */
    @Override
    public BusServiceItem selectBusServiceItemById(Long id) {
        return busServiceItemMapper.selectBusServiceItemById(id);
    }

    /**
     * 查询服务项列表
     *
     * @param busServiceItem 服务项
     * @return 服务项
     */
    @Override
    public List<BusServiceItem> selectBusServiceItemList(BusServiceItem busServiceItem) {
        return busServiceItemMapper.selectBusServiceItemList(busServiceItem);
    }

    /**
     * 新增服务项
     *
     * @param busServiceItemVo 服务项
     * @return 结果
     */
    @Override
    public int insertBusServiceItem(BusServiceItemVo busServiceItemVo) {
        Assert.state((busServiceItemVo.getOriginalPrice().compareTo(busServiceItemVo.getDiscountPrice())) >= 0, "服务项折扣价不能大于原价");
        BusServiceItem busServiceItem = new BusServiceItem();
        BeanUtils.copyBeanProp(busServiceItem, busServiceItemVo);
        if (busServiceItemVo.getCarPackage().equals(BusServiceItemVo.PACKAGE_NO)) {
            busServiceItem.setAuditStatus(ServiceItemAuditStatusEnum.NONE.ordinal());
        } else {
            busServiceItem.setAuditStatus(ServiceItemAuditStatusEnum.INITIALING.ordinal());
        }
        busServiceItem.setSaleStatus(BusServiceItemVo.SALI_NO);
        busServiceItem.setCreateTime(DateUtils.getNowDate());
        return busServiceItemMapper.insertBusServiceItem(busServiceItem);
    }

    /**
     * 修改服务项
     *
     * @param busServiceItemVo 服务项
     * @return 结果
     */
    @Override
    public int updateBusServiceItem(BusServiceItemVo busServiceItemVo) {
        Assert.notNull(busServiceItemVo.getId(), "请选择要修改的服务项");
        Assert.state((busServiceItemVo.getOriginalPrice().compareTo(busServiceItemVo.getDiscountPrice())) >= 0, "服务项折扣价不能大于原价");
        BusServiceItem busServiceItem = selectBusServiceItemById(busServiceItemVo.getId());
        Assert.state(!busServiceItem.getAuditStatus().equals(AUDITING.ordinal()) &&
                busServiceItem.getSaleStatus() != BusServiceItemVo.SALE_YES, "当前服务项不能修改");
        if (busServiceItem.getAuditStatus().equals(PASSED.ordinal()) ||
                busServiceItem.getAuditStatus().equals(NONE.ordinal()) ||
                busServiceItem.getAuditStatus().equals(INITIALING.ordinal())) {
            if (busServiceItemVo.getCarPackage().equals(BusServiceItemVo.PACKAGE_YES)) {
                busServiceItem.setAuditStatus(INITIALING.ordinal());
            } else {
                busServiceItem.setAuditStatus(NONE.ordinal());
            }
        }
        BeanUtils.copyBeanProp(busServiceItem, busServiceItemVo);
        return busServiceItemMapper.updateBusServiceItem(busServiceItem);
    }

    /**
     * 批量删除服务项
     *
     * @param ids 需要删除的服务项主键
     * @return 结果
     */
    @Override
    public int deleteBusServiceItemByIds(Long[] ids) {
        return busServiceItemMapper.deleteBusServiceItemByIds(ids);
    }

    /**
     * 删除服务项信息
     *
     * @param id 服务项主键
     * @return 结果
     */
    @Override
    public int deleteBusServiceItemById(Long id) {
        return busServiceItemMapper.deleteBusServiceItemById(id);
    }

    /**
     * 更新服务项上架状态
     *
     * @param id
     * @return
     */
    @Override
    public int updateSaleStatus(long id) {
        BusServiceItem busServiceItem = selectBusServiceItemById(id);
        Assert.notNull(busServiceItem, "参数错误");
        if (BusServiceItemVo.PACKAGE_YES.equals(busServiceItem.getCarPackage())) {
            Assert.state(busServiceItem.getAuditStatus().equals(PASSED.ordinal()), "审核通过才能上架");
        }
        long status = busServiceItem.getSaleStatus().equals(BusServiceItemVo.SALI_NO) ? BusServiceItemVo.SALE_YES : BusServiceItemVo.PACKAGE_NO;
        return busServiceItemMapper.updateSaleStatus(id, status);
    }

    /**
     * 根据服务项id查询审核信息
     *
     * @param id
     * @return
     */
    @Override
    public ServiceItemAuditInfo selectAuditInfoById(Long id) {
        ServiceItemAuditInfo serviceItemAuditInfo = new ServiceItemAuditInfo();
        BusServiceItem busServiceItem = selectBusServiceItemById(id);
        serviceItemAuditInfo.setBusServiceItem(busServiceItem);
        // TODO
        BigDecimal discountPrice = busBpmnInfoService.getCarPackageAuditDiscountPrice();
        serviceItemAuditInfo.setLimitDiscountPrice(discountPrice);
        List<SysUser> shopOwners = sysUserService.selectByRolekey("shopOwner");
        Assert.state(shopOwners.size() > 0, "请先添加店长");
        serviceItemAuditInfo.setShopOwners(shopOwners);
        if (busServiceItem.getDiscountPrice().compareTo(discountPrice) >= 0) {
            List<SysUser> financialList = sysUserService.selectByRolekey("financial");
            Assert.state(financialList.size() > 0, "请先添加财务");
            serviceItemAuditInfo.setFinances(financialList);
        }
        return serviceItemAuditInfo;
    }
/**
 * 修改服务项的审核状态
 */
    @Override
    public void updateAuditStatus(Long id, int auditStatus) {
        busServiceItemMapper.updateAuditStatus(id,auditStatus);
    }
}
